﻿using Hander.Customization;
using Hander.Forms.Frame;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Nut.utils;
using Hander.Forms.Frame.TestFrm;
using Hander.Customization.Frame.Classification;
using CharmNotification;
using Hander.Forms;

namespace Hander
{
    public partial class HanderMain : FunctionFrm
    {
        #region log4net
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion
        #region XmlConfigUtil
        XmlConfigUtil xml = new XmlConfigUtil();
        #endregion
        public HanderMain()
        {
            InitializeComponent();
            InitEnv();

        }

        #region initial
        /// <summary>
        /// 初始化
        /// </summary>
        public void InitEnv()
        {
            Global.Root_dir = Application.StartupPath;
            //Global.ChangeToSame();
            xml.XmlPath = string.Join(Path.DirectorySeparatorChar.ToString(), Global.Conf_subdir, Global.File_conf_xml);
            //xml.Write($"{Global.Conf_subdir}{Path.DirectorySeparatorChar}DockPanel.xml", "DockFrame", "Layout");
            string _log_path = string.Join(Path.DirectorySeparatorChar.ToString(), Global.Root_dir, Global.Log_subdir);
            FileCookie.CreateDirYes(_log_path);
            xml.Write(_log_path, "Logg4net", "LogAbsPath");
            xml.Write(Global.Log_subdir, "Logg4net", "LogPath");


            _log_path = string.Join(Path.DirectorySeparatorChar.ToString(), Global.Conf_subdir, Global.File_log_xml);
            xml.Write(_log_path, "Logg4net", "LogConfFile");
            log4net.Config.XmlConfigurator.Configure(new FileInfo(_log_path));
            foreach (log4net.Appender.IAppender app in log4net.LogManager.GetRepository().GetAppenders())
            {
                log.Debug(app.Name);
                log.Debug(app.GetType());
                if (app.Name.Equals("RollingLogFileAppender"))
                {
                    var _app = app as log4net.Appender.RollingFileAppender;
                    FileInfo _f = new FileInfo(_app.File);
                    _app.File = string.Join(Path.DirectorySeparatorChar.ToString(), Global.Log_subdir, _f.Name);
                    _app.ActivateOptions();
                }
                else if (app.Name.Equals("LogFileAppender"))
                {
                    var _app = app as log4net.Appender.FileAppender;
                    FileInfo _f = new FileInfo(_app.File);
                    _app.File = string.Join(Path.DirectorySeparatorChar.ToString(), Global.Log_subdir, _f.Name);
                    _app.ActivateOptions();
                }
            }
            log.Debug($"应用配置文件：{xml.XmlPath}");


            log.Debug($"log4net配置文件：{_log_path}");

            ///

            LiteHelper.InitLiteDB(xml.Read(Global.default_litedb));

            //



        }

        #endregion

        private void Nut_Dock_Btn_Click(object sender, EventArgs e)
        {
            var _dockmain = new DockMain();
            _dockmain.Show();
        }

        private void Test_Btn_Click(object sender, EventArgs e)
        {
            ClassDictionary _class = new ClassDictionary();
            Type _type = _class.GetType();
            PropertyInfo[] _ps = _type.GetProperties();
            foreach (PropertyInfo _p in _ps)
            {
                object _o=_p.GetValue(_class);

                Console.WriteLine(_o?.ToString());



            }
        }

        private void Key_Txtbox_TextChanged(object sender, EventArgs e)
        {
            var t = sender as TextBox;
            Global.Default_pwd = t.Text;
        }

        private void UserName_Txtbox_TextChanged(object sender, EventArgs e)
        {

        }

        private void UserName_Txtbox_Click(object sender, EventArgs e)
        {

        }

        private void UserName_Txtbox_Enter(object sender, EventArgs e)
        {
            var t = sender as TextBox;
            if (t.Text.Equals(MsgTip.tip_name))
            {
                t.Text = "";
            }
        }

        private void UserName_Txtbox_Leave(object sender, EventArgs e)
        {
            var t = sender as TextBox;
            if (t.Text.Equals(""))
            {
                t.Text = MsgTip.tip_name;
            }
        }

        private void HanderMain_Load(object sender, EventArgs e)
        {
            UserName_Txtbox.Text = MsgTip.tip_name;
        }

        private void TestComboxBtn_Click(object sender, EventArgs e)
        {
            TestCustomBoxFrm t = new TestCustomBoxFrm();
            t.Show();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            MessageAsync("测试123456", "测试内容12345678901234567890\n\t\r");
        }

       
    }
}
